package com.mioto.ld.module.product.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.mioto.ld.module.center.model.DeviceSaleAreaVO;
import com.mioto.ld.module.center.model.DeviceStatusVO;
import com.mioto.ld.module.center.model.ProductSaleRankVO;
import com.mioto.ld.module.center.model.ProductSaleTrendVO;
import com.mioto.ld.module.product.dao.*;
import com.mioto.ld.module.product.model.*;
import com.mioto.ld.module.product.service.IProductAttributeService;
import com.mioto.ld.module.product.service.IProductEventService;
import com.mioto.ld.module.product.service.IProductFunctionService;
import org.springframework.stereotype.Service;

import com.mioto.ld.module.product.service.IProductService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 *
 * @author mioto
 * @date 2024-05-14 21:05:44
 */
@Service("productService")
public class ProductServiceImpl implements IProductService{

    @Resource
    private ProductDao productDao;
    @Resource
    private IProductEventService productEventService;
    @Resource
    private IProductAttributeService productAttributeService;
    @Resource
    private IProductFunctionService productFunctionService;

    @Override
    public List<Product> findList(Product product) {
        return productDao.findList(product);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insert(ProductEditDTO product) {
        product.initCreateData();
        int result = productDao.insertIgnoreNull(product);
        if (CollUtil.isNotEmpty(product.getProductEvents())) {
            product.getProductEvents().forEach(p -> {
                p.setProductId(product.getId());
                productEventService.insert(p);
            });
        }
        if (CollUtil.isNotEmpty(product.getProductAttributes())) {
            product.getProductAttributes().forEach(p -> {
                p.setProductId(product.getId());
                productAttributeService.insert(p);
            });
        }
        if (CollUtil.isNotEmpty(product.getProductFunctions())) {
            product.getProductFunctions().forEach(p -> {
                p.setProductId(product.getId());
                productFunctionService.insert(p);
            });
        }
        return result;
    }

    @Override
    public int update(Product product) {
        return productDao.update(product);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateIgnoreNull(ProductEditDTO product) {
        if (ObjectUtil.isNotNull(product.getId())) {
            productDao.updateIgnoreNull(product);
        }
        productEventService.edit(product.getProductEvents());
        productAttributeService.edit(product.getProductAttributes());
        productFunctionService.edit(product.getProductFunctions());
        return 1;
    }

    @Override
    public Product findByColumn(String column, Object value) {
        return productDao.findByColumn(column,value);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteById(int value) {
        productEventService.deleteByColumn("product_id",value);
        productFunctionService.deleteByColumn("product_id",value);
        productAttributeService.deleteByColumn("product_id",value);
        return productDao.deleteByColumn("id",value);
    }

    @Override
    public int batchDelete(Integer[] ids) {
        return productDao.batchDelete(ids);
    }

    @Override
    public List<ProductPagerVO> findPager(Product product) {
        return productDao.findPager(product);
    }

    @Override
    public ProductDetailVO findById(int id) {
        return productDao.findById(id);
    }

    @Override
    public List<ProductSaleRankVO> productSaleStatistics() {
        return productDao.productSaleStatistics();
    }

    @Override
    public List<ProductSaleTrendVO> saleTrendStatistics(Date startDate, Date endDate) {
        return productDao.saleTrendStatistics(startDate,endDate);
    }

    @Override
    public List<DeviceSaleAreaVO> saleAreaStatistics() {
        return productDao.saleAreaStatistics();
    }
}